package com.example.demo.controller.user;

import com.example.demo.entity.member.User;
import com.example.demo.service.SmsService;
import com.example.demo.service.member.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RequestMapping("/user")
@Controller
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private SmsService smsService;

    @GetMapping("/reg")
    public String reg() {
        return "/user/reg";
    }

    //手机注册
    @PostMapping("/regByPhone")
    public String regByPhone(User user, String verificationCode, Model model) {
        String result = userService.regByPhone(user,verificationCode);
        model.addAttribute("result",result);
        return "/user/reg_result";
    }
    @PostMapping("/sendVerificationCode")
    @ResponseBody
    public String sendVerificationCode(String mobile) throws Exception {
        try {
            String statusCode = smsService.send(mobile);
            if("000000".equals(statusCode)) {
                return "success";
            } else {
                return "error";
            }
        } catch (Exception e) {
            return "error";
        }

    }
    //邮箱注册
    @PostMapping("/regByEmail")
    public String regByEmail(User user, Model model) {
        String result = userService.regByEmail(user);
        model.addAttribute("result",result);
        return "/user/reg_result";
    }
    @GetMapping("/activeuserEmail")
    public String activeuserEmail(String sid,Model model) {
        String result = userService.activeuserEmail(sid);
        model.addAttribute("result",result);
        return "/user/reg_result";
    }
    //用户名注册
    @PostMapping("/regByName")
    public String regByName(User user, Model model) {
        String result = userService.regByName(user);
        model.addAttribute("result",result);
        return "/user/reg_result";
    }
    @GetMapping("/login")
    public String login() {
        return "/user/login";
    }
    /*@PostMapping("/login")
    public String login(User user, HttpSession session,Model model) {
        String result = userService.login(user);
        model.addAttribute("result",result);
        if("success".equals(result)) {
            session.setAttribute("loginUser", user);
            return "redirect:/";
        }
        return "/user/login";
    }*/
}
